var $,
    form,
    layer;

var user = localStorage.getItem('user');
layui.use(['jquery', 'layer', 'form'], function () {
   $ = layui.jquery,
       form = layui.form,
       layer = layui.layer;
});

getTableData();
function getTableData(categroyId, productId, productName, productLabel, productSate, productCode, currentpriceProduct, searchKeywords) {
    $.ajax({
        url:'/Product/getByUserIdProductLists',
        type: 'post',
        dataType: 'json',
        data: {
            'categroyId' : categroyId,
            'productId' : productId,
            'productName' : productName,
            'productLabel' : productLabel,
            'productSate' : productSate,
            'productCode' : productCode,
            'currentpriceProduct' : currentpriceProduct,
            'searchKeywords' : searchKeywords,
            'userId': user.userId,
            'type' : 3
        },
        success: function (item) {
            getTable(item.data);
        }
    });
};


//查询表
function getTable(data) {
    layui.use(['jquery', 'layer', 'table'], function () {
        table = layui.table;

        table.render({
            elem: '#demo',
            data: data,
            toolbar:'#toolutil',
            page:true,
            limit:5,
            text: {
                none: '暂无相关数据' //默认：无数据。
            },
            cols:[[
                {checkbox:true},
                {field:'productCode',title:'商品编号',align:'center',height:'20px'},
                {title:'商品图片',align:'center',templet:function(data){
                        return "<img src='"+data.mainImage+"'width='80px' height='80px'/>";
                    }},
                {field:'productName',title:'商品名称',align:'center'},
                {field:'currentpriceProduct',title:'商品价格',align:'center'},
                {title:'上架/下架',align:'center',templet:function(data){
                        if(data.productSate === 1){
                            return"<input type='checkbox' value='"+data.productId+"' lay-filter='swi' lay-skin='switch' lay-text='上架|下架' checked>";
                        }else if(data.productSate === 0){
                            return "<span>已删除</span>";
                        }else{
                            return"<input type='checkbox' value='"+data.productId+"' lay-filter='swi' lay-skin='switch' lay-text='上架|下架'>";
                        }
                    }},
                {field:'title',title:'商品分类',align:'center'},
                {field:'productLabel',title:'商品标签',align:'center'},
                {field:'result',title:'审核状态',align:'center',templet:function(data){
                        var result;
                        if(data.result == 1){
                            result = "审核通过";
                        }else if(data.result == 2){
                            result = "待审核";
                        }else if(data.result == 3){
                            result = "审核驳回";
                        }else{
                            result = "";
                        }
                        return "<div>"+result+"</div>" +
                            "<a class='operation' onclick='auditDetails("+data.productId+")'>审核详情</a>";
                    }},
                {field:'title',title:'操作',align:'center',templet:function(data){
                        return "<a class='operation' onclick='details("+data.productId+")'>查看</a>  <a class='operation' onclick='edit("+data.productId+")'>编辑</a> <br/>"+
                            "<a class='operation' onclick='commodityLog("+data.productId+")'>日志</a>  <a onclick='delProduct(\""+data.productId+"\",\""+data.productCode+"\")' class='operation'>删除</a>";
                    }},
            ]]
        });
    });

};

//查看商品详情信息
function details (id) {
    layer.open({
        type: 2,
        offset: 'auto',
        title: '商品详情',
        resieze: false,
        area: ['1100px', '500px'],
        content: '/product/productList-layer/productDetails.html?tx=' + encodeURI(id)
    });
};

//修改商品信息
function edit(id) {
    layer.open({
        type: 2,
        offset: 'auto',
        title: '商品详情',
        resieze: false,
        area: ['680px', '550px'],
        content:'/product/productList-layer/productlist-modify.html?tx=' +　encodeURI(id)
    });
};

function addProductOperationRecord(operationType, executionTime, remarks) {
    // 添加的操作日志
    $.ajax({
        url:'/Product/addProductOperationRecord',
        dataType:'json',
        data:{
            "operationType": operationType,
            "executionTime": executionTime,
            "remarks": remarks,
            "updateId": user.userId,
            "operationList": 'js_product',
            "type": 1
        }
    });
};

//搜索
function selProduct () {
    var data = form.val("formTest");
    getTableData('', '', data.productName, '', '', '', data.currentpriceProduct, data.searchKeywords)
}

//高级检索
function accurate () {
    layer.open({
        type: 2,
        offset: 'auto',
        title: '信息',
        resize: false,
        area: ['600px', '500px'],
        content: '/product/productList-layer/productlist-precisesearch.html'
    });
}


//查看审核信息
function auditDetails (id) {
    layer.open({
        type:2,
        offset: 'auto',
        title: '审核详情',
        resize: false,
        area:['1020px','500px'],
        content:'/product/productList-layer/product-auditDetails.html?tx=' + encodeURI(id)
    });
};

//获取时间
function getDate() {
    var date = new Date().toLocaleString('zh', { hour12: false }).split('/').join('-');
    var temp = date.substring(0, 9);
    date = date.substr(date.lastIndexOf('-') + 3);
    date = temp + date;
    return date;
}


//刷新页面
function refresh (){
    location.reload();
}

//返回首页
function backOff(){
    parent.jumpSystemHome();
}